<template>
    <header>
        <van-nav-bar title="注册" left-text="返回" left-arrow @click-left="$router.back()" />
    </header>
    <div class="content">
        <van-form @submit="onSubmit" style="margin-top: 20px;">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="请输入手机号"
                    :rules="[{ required: true, message: '请填写手机号' }, { pattern: /^1(3|4|5|7|8)\d{9}$/, message: '请输入正确的手机号' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import { showSuccessToast, showFailToast } from 'vant';
import request from '@/utils/request'
export default {
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        onSubmit(values) {
            console.log(values)
            request.post('/register', values).then(res => {
                if (res.data.code == 200) {
                    // 注册成功 
                    showSuccessToast(res.data.message);
                    // 跳转到登录页面
                    this.$router.replace('/login')
                } else {
                    // 注册失败
                    showFailToast(res.data.message);
                }
            })
        }
    }
}
</script>